package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.entity.Book;
import com.imooc.entity.Evaluation;
import com.imooc.entity.Member;
import com.imooc.exception.BusinessException;
import com.imooc.exception.BusinessExceptionCode;
import com.imooc.mapper.BookMapper;
import com.imooc.mapper.EvaluationMapper;
import com.imooc.mapper.MemberMapper;
import com.imooc.service.EvaluationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("evaluationService")
@Transactional
public class EvaluationServiceImpl implements EvaluationService {
    @Resource
    private EvaluationMapper evaluationMapper;

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private BookMapper bookMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<Evaluation> selectByBookId(Long bookId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookId);
        queryWrapper.eq("state", "enable");
        queryWrapper.orderByDesc("create_time");
        List<Evaluation> evaluations = evaluationMapper.selectList(queryWrapper);
        for (Evaluation evaluation: evaluations) {
            Member member = memberMapper.selectById(evaluation.getMemberId());
            evaluation.setMember(member);
        }
        for (Evaluation evaluation: evaluations) {
            Book book = bookMapper.selectById(evaluation.getBookId());
            evaluation.setBook(book);
        }
        return evaluations;
    }

    @Override
    public Evaluation evaluate(Long memberId, Long bookId, Integer score, String content) {
        Evaluation evaluation = new Evaluation();
        evaluation.setMemberId(memberId);
        evaluation.setBookId(bookId);
        evaluation.setContent(content);
        evaluation.setCreateTime(new Date());
        evaluation.setScore(score);
        evaluation.setState("enable");
        evaluation.setEnjoy(0);
        evaluationMapper.insert(evaluation);
        return evaluation;
    }

    @Override
    public Evaluation enjoy(Long evaluationId) {
        Evaluation evaluation = evaluationMapper.selectById(evaluationId);
        if (evaluation == null) {
            throw new BusinessException(BusinessExceptionCode.EVALUATION_NOT_FOUND);
        }
        evaluation.setEnjoy(evaluation.getEnjoy()+1);
        evaluationMapper.updateById(evaluation);
        return evaluation;
    }

    @Override
    public IPage<Evaluation> paging(Integer page, Integer rows) {
        Page<Evaluation> evaluationPage = new Page<>(page, rows);
        QueryWrapper<Evaluation> wrapper = new QueryWrapper<>();
        Page<Evaluation> evaluationIPage = evaluationMapper.selectPage(evaluationPage, wrapper);
        List<Evaluation> evaluations = evaluationIPage.getRecords();
        for (Evaluation evaluation: evaluations) {
            Member member = memberMapper.selectById(evaluation.getMemberId());
            evaluation.setMember(member);
        }
        for (Evaluation evaluation: evaluations) {
            Book book = bookMapper.selectById(evaluation.getBookId());
            evaluation.setBook(book);
        }
        return evaluationIPage;
    }

    @Override
    public void disableEvaluation(Long evaluationId, String reason) {
        Evaluation evaluation = evaluationMapper.selectById(evaluationId);
        if (evaluation == null) {
            throw new BusinessException(BusinessExceptionCode.MANAGE_EVALUATION_NOT_FOUND);
        }
        if (reason == null || "".equals(reason)) {
            throw new BusinessException(BusinessExceptionCode.MANAGE_EVALUATION_REASON_NOT_EMPTY);
        }
        evaluation.setState("disable");
        evaluation.setDisableReason(reason);
        evaluationMapper.updateById(evaluation);
    }
}
